package com.iraeta.controller.gift;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.common.Result;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.Role;
import com.iraeta.entity.TreeNode;
import com.iraeta.entity.gift.GiftType;
import com.iraeta.entity.vo.GiftTypeTreeNode;
import com.iraeta.service.gift.IGiftTypeService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController(value = "GiftTypeController")
@RequestMapping("gift/gifttype")
public class GiftTypeController extends BaseController {
    @Resource
    IGiftTypeService giftTypeService;

    @RequestMapping("getgifttypetreenode")
    public String getGiftTypeTreeNode(){
        List<GiftTypeTreeNode> giftTypeTreeNodeList  = giftTypeService.getGiftTypeTreeNode();
        return this.success(giftTypeTreeNodeList);
    }

    @RequestMapping("getgifttype")
    public String getGiftType(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        GiftType giftType = giftTypeService.getById(id);
        return this.success(giftType);
    }

    @RequestMapping("addeidtgifttype")
    public String addEditGiftType(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        GiftType giftType = id == 0 ? new GiftType() :  giftTypeService.getById(id);
        this.mapJsonToObj(paramJb, giftType);
        if(id == 0){
            if(giftType.getPid() == null) {
                giftType.setPid(0);
                giftTypeService.save(giftType);
                giftType.setMember(giftType.getId().toString());
                giftTypeService.updateById(giftType);
            }else{
                GiftType pGiftType = giftTypeService.getById(giftType.getPid());
                giftTypeService.save(giftType);
                giftType.setMember(pGiftType.getMember() + "," + giftType.getId().toString());
                giftTypeService.updateById(giftType);
            }
        }else {
            giftTypeService.updateById(giftType);
        }
        return this.success();
    }

    @RequestMapping("deletegifttype")
    public String deleteGiftType(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        giftTypeService.deleteGiftType(id);
        return this.success();
    }

    @RequestMapping("gettreenode")
    public String getTreeNode(){
        List<TreeNode> treeNodeList  = giftTypeService.getTreeNode();
        return this.success(treeNodeList);
    }
}
